<template>
  <div class="student-error-card">
    <!-- 卡片头部 -->
    <div class="card-header">
      <div class="header-left">
        <h3 class="error-title">{{ error.title }}</h3>
        <div class="error-meta">
          <span class="task-name">{{ error.taskName }}</span>
          <span class="error-date">{{ formatDate(error.errorDate) }}</span>
        </div>
      </div>
      <div class="header-right">
        <div class="status-badge" :class="getStatusClass(error.status)">
          {{ getStatusText(error.status) }}
        </div>
        <div class="difficulty-badge" :class="getDifficultyClass(error.difficulty)">
          {{ error.difficulty }}
        </div>
      </div>
    </div>

    <!-- 题目内容 -->
    <div class="question-content">
      <div class="question-text">{{ error.question }}</div>
    </div>

    <!-- 答案对比 -->
    <div class="answer-comparison">
      <div class="answer-item wrong">
        <div class="answer-label">我的答案：</div>
        <div class="answer-value">{{ error.wrongAnswer }}</div>
      </div>
      <div class="answer-item correct">
        <div class="answer-label">正确答案：</div>
        <div class="answer-value">{{ error.correctAnswer }}</div>
      </div>
    </div>

    <!-- 解析内容 -->
    <div class="explanation-content">
      <div class="explanation-label">解析：</div>
      <div class="explanation-text">{{ error.explanation }}</div>
    </div>

    <!-- 标签 -->
    <div class="tags-section">
      <div class="tag" v-for="tag in error.tags" :key="tag">
        {{ tag }}
      </div>
    </div>

    <!-- 统计信息 -->
    <div class="stats-section">
      <div class="stat-item">
        <span class="stat-label">复习次数：</span>
        <span class="stat-value">{{ error.reviewCount }}</span>
      </div>
      <div class="stat-item">
        <span class="stat-label">练习次数：</span>
        <span class="stat-value">{{ error.practiceCount }}</span>
      </div>
    </div>

    <!-- 操作按钮 -->
    <div class="card-actions">
      <el-button 
        v-if="error.status !== 'mastered'" 
        type="primary" 
        size="small" 
        @click="handleReview"
      >
        复习一下
      </el-button>
      
      <el-button 
        v-if="error.status === 'reviewing' || error.status === 'practiced'" 
        type="success" 
        size="small" 
        @click="handlePractice"
      >
        相似练习
      </el-button>
      
      <el-button 
        v-if="error.status !== 'mastered'" 
        type="info" 
        size="small" 
        @click="handleMaster"
      >
        已掌握
      </el-button>
      
      <el-button 
        v-if="error.status === 'mastered'" 
        type="success" 
        size="small" 
        disabled
      >
        ✓ 已掌握
      </el-button>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ElButton } from 'element-plus'

// 定义props
interface ErrorRecord {
  id: number
  title: string
  question: string
  subject: string
  status: string
  difficulty: string
  wrongAnswer: string
  correctAnswer: string
  explanation: string
  taskName: string
  errorDate: Date
  reviewCount: number
  practiceCount: number
  tags: string[]
}

interface Props {
  error: ErrorRecord
}

const props = defineProps<Props>()

// 定义emits
const emit = defineEmits<{
  review: [error: ErrorRecord]
  practice: [error: ErrorRecord]
  master: [error: ErrorRecord]
}>()

// 获取状态样式类
const getStatusClass = (status: string) => {
  const statusMap: Record<string, string> = {
    'new': 'status-new',
    'reviewing': 'status-reviewing',
    'practiced': 'status-practiced',
    'mastered': 'status-mastered'
  }
  return statusMap[status] || 'status-default'
}

// 获取状态文本
const getStatusText = (status: string) => {
  const statusMap: Record<string, string> = {
    'new': '新错题',
    'reviewing': '复习中',
    'practiced': '已练习',
    'mastered': '已掌握'
  }
  return statusMap[status] || status
}

// 获取难度样式类
const getDifficultyClass = (difficulty: string) => {
  const difficultyMap: Record<string, string> = {
    '简单': 'difficulty-easy',
    '中等': 'difficulty-medium',
    '困难': 'difficulty-hard'
  }
  return difficultyMap[difficulty] || 'difficulty-medium'
}

// 格式化日期
const formatDate = (date: Date) => {
  return date.toLocaleDateString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit'
  })
}

// 处理操作事件
const handleReview = () => {
  emit('review', props.error)
}

const handlePractice = () => {
  emit('practice', props.error)
}

const handleMaster = () => {
  emit('master', props.error)
}
</script>

<style scoped>
.student-error-card {
  background: #fff;
  border-radius: 8px;
  padding: 20px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
  border: 1px solid #f0f0f0;
  transition: all 0.3s ease;
}

.student-error-card:hover {
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1);
  border-color: #e0e0e0;
}

/* 卡片头部 */
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 16px;
}

.header-left {
  flex: 1;
  min-width: 0;
}

.error-title {
  font-size: 16px;
  font-weight: 600;
  color: #303133;
  margin: 0 0 4px 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.error-meta {
  display: flex;
  gap: 12px;
  font-size: 13px;
  color: #909399;
}

.task-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.header-right {
  display: flex;
  flex-direction: column;
  gap: 8px;
  align-items: flex-end;
  flex-shrink: 0;
  margin-left: 12px;
}

/* 状态和难度标签 */
.status-badge,
.difficulty-badge {
  padding: 4px 8px;
  border-radius: 12px;
  font-size: 12px;
  font-weight: 500;
  white-space: nowrap;
}

.status-new {
  background: #fef0f0;
  color: #f56c6c;
}

.status-reviewing {
  background: #fdf6ec;
  color: #e6a23c;
}

.status-practiced {
  background: #e1f3ff;
  color: #409eff;
}

.status-mastered {
  background: #f0f9ff;
  color: #67c23a;
}

.difficulty-easy {
  background: #f0f9ff;
  color: #67c23a;
}

.difficulty-medium {
  background: #fdf6ec;
  color: #e6a23c;
}

.difficulty-hard {
  background: #fef0f0;
  color: #f56c6c;
}

/* 题目内容 */
.question-content {
  margin-bottom: 16px;
  padding: 12px;
  background: #f8f9fa;
  border-radius: 6px;
  border-left: 4px solid #409eff;
}

.question-text {
  font-size: 14px;
  color: #303133;
  line-height: 1.5;
}

/* 答案对比 */
.answer-comparison {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin-bottom: 16px;
}

.answer-item {
  padding: 12px;
  border-radius: 6px;
  font-size: 14px;
}

.answer-item.wrong {
  background: #fef0f0;
  border-left: 4px solid #f56c6c;
}

.answer-item.correct {
  background: #f0f9ff;
  border-left: 4px solid #67c23a;
}

.answer-label {
  font-weight: 600;
  color: #606266;
  margin-bottom: 4px;
}

.answer-value {
  color: #303133;
  line-height: 1.4;
}

/* 解析内容 */
.explanation-content {
  margin-bottom: 16px;
  padding: 12px;
  background: #fafafa;
  border-radius: 6px;
  border-left: 4px solid #909399;
}

.explanation-label {
  font-size: 13px;
  font-weight: 600;
  color: #606266;
  margin-bottom: 4px;
}

.explanation-text {
  font-size: 14px;
  color: #303133;
  line-height: 1.5;
}

/* 标签 */
.tags-section {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 16px;
}

.tag {
  padding: 2px 8px;
  background: #ecf5ff;
  color: #409eff;
  border-radius: 12px;
  font-size: 12px;
  border: 1px solid #b3d8ff;
}

/* 统计信息 */
.stats-section {
  display: flex;
  gap: 16px;
  margin-bottom: 16px;
  font-size: 13px;
}

.stat-item {
  display: flex;
  align-items: center;
  gap: 4px;
}

.stat-label {
  color: #909399;
}

.stat-value {
  color: #409eff;
  font-weight: 600;
}

/* 操作按钮 */
.card-actions {
  display: flex;
  gap: 8px;
  padding-top: 16px;
  border-top: 1px solid #f0f0f0;
  flex-wrap: wrap;
}

.card-actions .el-button {
  font-size: 13px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .student-error-card {
    padding: 16px;
  }

  .card-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 12px;
  }

  .header-right {
    flex-direction: row;
    align-items: center;
    margin-left: 0;
  }

  .answer-comparison {
    grid-template-columns: 1fr;
  }

  .stats-section {
    flex-direction: column;
    gap: 8px;
  }

  .card-actions {
    gap: 6px;
  }

  .card-actions .el-button {
    font-size: 12px;
    padding: 6px 12px;
  }
}

@media (max-width: 480px) {
  .error-title {
    font-size: 15px;
  }

  .question-text,
  .answer-value,
  .explanation-text {
    font-size: 13px;
  }
}
</style>
